//
//  NSString+HanZiToPingYing.m
//  lc-collect
//
//  Created by mac on 2021/3/3.
//

#import "NSString+HanZiToPingYing.h"

@implementation NSString (HanZiToPingYing)

- (NSString*)pinYin {
    
    //先转换为带声调的拼音
    NSMutableString *str = [self mutableCopy];
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    
    return str;
}

/// 获取拼音首字母
- (NSString*)firstCharactor {
    
    //1.先传化为拼音
    NSString*pinYin = [self.pinYin uppercaseString];
    //2.获取首字母
    if (!pinYin || [pinYin isBlank]) {
        return @"#";
    }
    return [pinYin substringToIndex:1];
}

/// 获取汉字拼音，包括判断返回#
- (NSString*)firstPingYin {
    
    //1.先传化为拼音
    NSString * pinYin = [self.pinYin uppercaseString];
    //2.获取首字母
    if (!pinYin || [pinYin isBlank]) {
        return @"#";
    }
    pinYin = [pinYin substringToIndex:1];
    if ([pinYin compare:@"A"] == NSOrderedAscending || [pinYin compare:@"Z"] == NSOrderedDescending) {
        pinYin = @"#";
    }
    return pinYin;
}

- (BOOL)isBlank {
    
    if (self == nil || self == NULL || [self isKindOfClass:[NSNull class]] || [self length] == 0 || [self isEqualToString: @"(null)"]) {
        return YES;
    }
    
    return NO;
}

@end
